﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Card.Web
{
    public partial class Register : BasePage
    {
        #region 方法
        protected override void OnInit(EventArgs e)
        {
            this.btnReg.Click += new EventHandler(btnReg_Click);
            base.OnInit(e);
        }

        void btnReg_Click(object sender, EventArgs e)
        {
            if (DoRegisteCheck())
            {
                Entity.Users user = new Card.Entity.Users();
                user.ID = BAF.BLL.SYSSequenceManager.GetSequence(CONST.SEQUENCE_CODE.USER_ID, null);
                user.UserName = this.txtUserName.Text;
                user.Password = Security.Utility.EncryptPassWord(this.txtPW.Text.Trim());
                user.Email = this.txtMail.Text;
                user.Status = CONST.YES_NO.YES;
                user.LastLoginDate = DateTime.Now;
                BLL.UsersManager.Add(user,this.Passport);
                string loginMessage = string.Empty;
                Common.LoginUtility.Login(user.UserName, this.txtPW.Text.Trim(), true, ref loginMessage, this.Passport);
                Response.Redirect("/");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region 方法

        private bool DoRegisteCheck()
        {
            if (string.IsNullOrEmpty(this.txtUserName.Text))
            {
                this.MessageBox("用户名不能为空！");
                return false;
            }

            if (BLL.UsersManager.GetUserByUName(this.txtUserName.Text,this.Passport) != null)
            {
                this.MessageBox("用户名已经存！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtPW.Text) || this.txtPW.Text.Length < 6)
            {
                this.MessageBox("密码不能为空，且长度不少于6个字符！");
                return false;
            }

            if (string.Compare(this.txtPW.Text, this.txtRePW.Text) != 0)
            {
                this.MessageBox("两次输入的密码不一致！");
                return false;
            }

            return true;
        }

        #endregion
    }
}
